﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Chapter17.View
{
    using System.Reflection;
    using System.Collections;
    using System.Xml;
    using System.Windows.Markup;

    /// <summary>
    /// ControlTemplateBrowse.xaml 的交互逻辑
    /// </summary>
    public partial class ControlTemplateBrowseWindow : Window
    {
        public ControlTemplateBrowseWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var controlType = typeof(UIElement);
            var derivedTypes = new List<Type>();
            var assembly = Assembly.GetAssembly(typeof(Control));
            foreach (var type in assembly.GetTypes())
            {
                if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic)
                {
                    derivedTypes.Add(type);
                }
            }
            derivedTypes.Sort(new TypeComparer());
            listbox.ItemsSource = derivedTypes;
        }

        private class TypeComparer : IComparer<Type>
        {
            public int Compare(Type x, Type y)
            {
                return x.Name.CompareTo(y.Name);
            }
        }

        private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var type = (Type)listbox.SelectedItem;
                var info = type.GetConstructor(System.Type.EmptyTypes);
                var control = (Control)info.Invoke(null);
                var win = control as Window;
                if (win != null)
                {
                    win.WindowState =WindowState.Minimized;
                    win.ShowInTaskbar = false;
                    win.Show();
                }
                else
                {
                    control.Visibility = Visibility.Collapsed;
                    grid.Children.Add(control);
                }
                var template = control.Template;
                var settings = new XmlWriterSettings();
                settings.Indent = true;
                var sb = new StringBuilder();
                var writer = XmlWriter.Create(sb, settings);
                XamlWriter.Save(template, writer);
                textbox.Text = sb.ToString();
                if (win != null)
                {
                    win.Close();
                }
                else
                {
                    grid.Children.Remove(control);
                }
            }
            catch (Exception err)
            {
                textbox.Text = "<< Error generating template: " + err.Message + ">>";
            }
        }
    }
}
